package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.ruoyi.system.domain.summary.AnnualMetrics;
import com.ruoyi.system.domain.summary.ProjectSummary;
import com.ruoyi.system.domain.summary.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectInfoMapper;
import com.ruoyi.system.domain.ProjectInfo;
import com.ruoyi.system.service.IProjectInfoService;

/**
 * 项目Service业务层处理
 * 
 * @author xuliang
 * @date 2024-03-29
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService 
{
    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    /**
     * 查询项目
     * 
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public ProjectInfo selectProjectInfoById(Long id)
    {
        return projectInfoMapper.selectProjectInfoById(id);
    }

    /**
     * 查询项目列表
     * 
     * @param projectInfo 项目
     * @return 项目
     */
    @Override
    public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo)
    {
        return projectInfoMapper.selectProjectInfoList(projectInfo);
    }

    /**
     * 新增项目
     * 
     * @param projectInfo 项目
     * @return 结果
     */
    @Override
    public int insertProjectInfo(ProjectInfo projectInfo)
    {
        return projectInfoMapper.insertProjectInfo(projectInfo);
    }

    /**
     * 修改项目
     * 
     * @param projectInfo 项目
     * @return 结果
     */
    @Override
    public int updateProjectInfo(ProjectInfo projectInfo)
    {
        return projectInfoMapper.updateProjectInfo(projectInfo);
    }

    /**
     * 批量删除项目
     * 
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public List<Long> deleteProjectInfoByIds(Long[] ids)
    {
        List<Long> failIds = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            if(checkProjectBeforeDelete(ids[i]) > 0){
                failIds.add(ids[i]);
            }else {
                deleteProjectInfoById(ids[i]);
            }
        }
        return failIds;
    }

    @Override
    public int checkProjectBeforeDelete(long id) {
        return projectInfoMapper.checkProjectBeforeDelete(id);
    }

    /**
     * 删除项目信息
     * 
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectInfoById(Long id)
    {
        return projectInfoMapper.deleteProjectInfoById(id);
    }

    // 查询时更新项目
    @Override
    public void updateProjectInfoBeforeGet(String projectName, String projectCode) {
        projectInfoMapper.updateProjectInfoBeforeGet(projectName, projectCode);
    }

    @Override
    public int checkProjectByNameAndCode(String projectName,String projectCode) {
        return projectInfoMapper.countProjectInfoByProjectCodeAndName(projectName,projectCode);
    }

    @Override
    public ProjectInfo selectProjectInfo(ProjectInfo projectInfo) {
        return projectInfoMapper.selectProjectInfo(projectInfo);
    }

    // 插入空汇总数据
    @Override
    public int insertItemSummary(String projectCode) {
        return projectInfoMapper.insertItemSummary(projectCode);
    }
    @Override
    public int insertLaborSummary(String projectCode) {
        return projectInfoMapper.insertLaborSummary(projectCode);
    }
    @Override
    public int insertDirectSummary(String projectCode) {
        return projectInfoMapper.insertDirectSummary(projectCode);

    }
    @Override
    public int insertIndirectSummary(String projectCode) {
        return projectInfoMapper.insertIndirectSummary(projectCode);
    }

    @Override
    public List<String> getProjectCodes(Integer areaId) {
        return projectInfoMapper.getProjectCodes(areaId);
    }

    @Override
    public List<String> getContractByAreaId(Integer areaId) {
        return projectInfoMapper.getContractByAreaId(areaId);
    }

    @Override
    public List<String> getContractNameByAreaId(Integer areaId) {
        return projectInfoMapper.getContractNameByAreaId(areaId);
    }

    // 更新汇总数据
    @Override
    public int updateItemSummary(String projectCode,boolean insert) {
        if(insert){
            CompletableFuture<Void> insertFuture = CompletableFuture.runAsync(() -> insertItemSummary(projectCode));
            insertFuture.join(); // 等待insert操作完成
        }
        return projectInfoMapper.updateItemSummary(projectCode);
    }
    @Override
    public int updateLaborSummary(String projectCode,boolean insert) {
        if(insert){
            CompletableFuture<Void> insertFuture = CompletableFuture.runAsync(() -> insertLaborSummary(projectCode));
            insertFuture.join(); // 等待insert操作完成
        }

        return projectInfoMapper.updateLaborSummary(projectCode);
    }
    @Override
    public int updateDirectSummary(String projectCode,boolean insert) {
        if(insert){
            CompletableFuture<Void> insertFuture = CompletableFuture.runAsync(() -> insertDirectSummary(projectCode));
            insertFuture.join(); // 等待insert操作完成
        }

        return projectInfoMapper.updateDirectSummary(projectCode);
    }
    @Override
    public int updateIndirectSummary(String projectCode,boolean insert) {
        if(insert){
            CompletableFuture<Void> insertFuture = CompletableFuture.runAsync(() -> insertIndirectSummary(projectCode));
            insertFuture.join(); // 等待insert操作完成
        }

        return projectInfoMapper.updateIndirectSummary(projectCode);
    }

    // 获取汇总数据
    @Override
    public Summary selectItemSummary(String projectCode) {
        CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> updateItemSummary(projectCode,true));
        try {
            // 等待更新操作完成
            updateFuture.get(); // 可以使用get方法等待异步操作完成
        } catch (InterruptedException | ExecutionException e) {
            // 处理异常
            e.printStackTrace();
        }
        // 查询并返回结果
        return projectInfoMapper.selectItemSummary(projectCode);
    }
    @Override
    public Summary selectLaborSummary(String projectCode) {
        CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> updateLaborSummary(projectCode,true));
        try {
            // 等待更新操作完成
            updateFuture.get(); // 可以使用get方法等待异步操作完成
        } catch (InterruptedException | ExecutionException e) {
            // 处理异常
            e.printStackTrace();
        }
        // 查询并返回结果
        return projectInfoMapper.selectLaborSummary(projectCode);
    }
    @Override
    public Summary selectDirectSummary(String projectCode) {
        CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> updateDirectSummary(projectCode,true));
        try {
            // 等待更新操作完成
            updateFuture.get(); // 可以使用get方法等待异步操作完成
        } catch (InterruptedException | ExecutionException e) {
            // 处理异常
            e.printStackTrace();
        }
        // 查询并返回结果
        return projectInfoMapper.selectDirectSummary(projectCode);
    }
    @Override
    public Summary selectIndirectSummary(String projectCode) {
        CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> updateIndirectSummary(projectCode,true));
        try {
            // 等待更新操作完成
            updateFuture.get(); // 可以使用get方法等待异步操作完成
        } catch (InterruptedException | ExecutionException e) {
            // 处理异常
            e.printStackTrace();
        }
        // 查询并返回结果
        return projectInfoMapper.selectIndirectSummary(projectCode);
    }


    @Override
    public int insertProjectSummary(ProjectSummary projectSummary) {
        return projectInfoMapper.insertProjectSummary(projectSummary);
    }

    @Override
    public int updateProjectSummary() {
        return projectInfoMapper.updateProjectSummary();
    }

    @Override
    public int updateProjectSummaryByEntity(ProjectSummary projectSummary) {
        return projectInfoMapper.updateProjectSummaryByEntity(projectSummary);
    }

    @Override
    public List<ProjectSummary> selectProjectSummaryList(Integer areaId) {
        return projectInfoMapper.selectProjectSummaryList(areaId);
    }

    @Override
    public int checkProjectSummary(String projectCode) {
        return projectInfoMapper.countProjectSummary(projectCode);
    }

    @Override
    public Integer selectMaxNumber(String projectCode) {
        return projectInfoMapper.selectMaxNumber(projectCode);
    }

    @Override
    public LocalDateTime selectMaxTimeByProjectCodeAndNumber(String projectCode, Integer maxNumber) {
        return projectInfoMapper.selectMaxTimeByProjectCodeAndNumber(projectCode,maxNumber);
    }

    @Override
    public int updateProjectNumber(String DateNumber,String projectCode) {
        return projectInfoMapper.updateProjectNumber(DateNumber,projectCode);
    }

    @Override
    public String getProjectCodeByName(String projectName) {
        return projectInfoMapper.selectProjectCodeByName(projectName);
    }

    @Override
    public BigDecimal getProjectContractByName(String projectName) {
        return projectInfoMapper.selectProjectContractByName(projectName);
    }

    @Override
    public int checkContractByNameAndCode(String projectName, String projectCode) {
        return projectInfoMapper.checkContractByNameAndCode(projectName,projectCode);
    }

    @Override
    public boolean checkRoleByUserId(long userId, List<Long> roleIds) {
        return projectInfoMapper.checkRoleByUserId(userId,roleIds);
    }

    @Override
    public List<Long> getRoleIdsByUserId(long userId) {
        return projectInfoMapper.selectRoleIdsByUserId(userId);
    }

    @Override
    public long getNewestUserId() {
        return projectInfoMapper.selectNewestUserId();
    }

    @Override
    public int insertProjectUser(String projectCode, long userId) {
        return projectInfoMapper.insertProjectUser(projectCode,userId);
    }

    @Override
    public String getProjectNameByCode(String projectCode) {
        return projectInfoMapper.selectProjectNameByCode(projectCode);
    }

    @Override
    public String getProjectCodeByUserId(long userId) {
        return projectInfoMapper.selectProjectCodeByUserId(userId);
    }

    @Override
    public int insertAnnualMetrics(Integer areaId, String dateNumber) {
        return projectInfoMapper.insertAnnualMetrics(areaId,dateNumber);
    }

    @Override
    public void updateAnnualMetrics() {
        projectInfoMapper.updateAnnualMetrics();
    }

    @Override
    public List<AnnualMetrics> getAnnualMetricsList(String dateNumber) {
        return projectInfoMapper.selectAnnualMetricsList(dateNumber);
    }


}
